$(function(){
	var agentInfo=common.getLocalData('H5-agentInfo');
	var token=common.getToken();
	var houseId=common.getQueryString('houseId');
	if(!houseId){
		window.location.href='./login.html';
	}
	var globalHost=common.globalHost;
	var addHouseVisitInfo=common.addHouseVisitInfo;
	var url=globalHost+addHouseVisitInfo;
	var $visitedPhotos=$("#visitedPhotos");
    var client = navigator.userAgent;
    var isiOS = !!client.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
	var $uploadHousePhotos=$('#upload-house-photos');
	if(isiOS){
		$uploadHousePhotos.find('input').attr('multiple',true);
	}
	var uploadOptions={
            uploadType:"more",
            fileName:"houseVisited",
            folder:"visit",
            maxlength:8,
            agentId:agentInfo.id,
            token:token,
            uploadSuccess:function (filePath,photoList) {
                $visitedPhotos.val(photoList.join(","));
            },
            deleteImage:function (photoList) {
                $visitedPhotos.val(photoList.join(","));
            },
            uploadFail:function (data) {

            }
        };
    var uploadHousePhoto= new UploadImage($uploadHousePhotos,uploadOptions);
    var $closeIt=$('#close-it');
    $closeIt.off('touchend').on('touchend',function(e){
    	var $this=$(this);
    	$this.parent().hide();
    	e.stopPropagation();
        e.preventDefault();
    	return false;
    });
	var $uploadImgBtn=$('#uploadImg-btn');
	var $imgBox=$('.img-box');
	/*判断上传图片的数量*/
	$uploadImgBtn.off('tap').on('tap',function(e){
		var $this=$(this);
		var checkCase=$this.hasClass("act-ago");
		var sendData={
			token:token,
			agentId:agentInfo.id,
			houseId:houseId,
			visitPhotos:$visitedPhotos.val()
		};
		var len=$imgBox.children().length;
		var valInputArr=$visitedPhotos.val().split(',');
		var valLength=valInputArr.length;
    	if(!!checkCase&&!!valLength&&valLength>0&&valLength===len-1){
    		$this.removeClass("act-ago").addClass("acted-yet");
    		common.requestFormData({
    			url:url,
    			sendData:sendData,
    			resSuccess:function(data){
    				common.errDialog("上传成功");
					$this.removeClass("acted-yet").addClass("act-ago");
                    setTimeout(function(){
                        window.location.href="./index.html";
                    },2000)	
    			},
    			resFailed:function(data){
                    data.code==2?common.errDialog("非悬赏房源"):common.errDialog("上传失败");
					$this.removeClass("acted-yet").addClass("act-ago");
    			},
    			resError:function(data){
					$this.removeClass("acted-yet").addClass("act-ago");
    			}
    		});
    	}else{
    		common.errDialog('请上传照片');
    	}
    	e.stopPropagation();
        e.preventDefault();
    	return false;
	})
})